package com.xing.crm.service.impl;

import com.xing.crm.mapper.ProRoleMapper;
import com.xing.crm.pojo.PageResult;
import com.xing.crm.pojo.ProRole;
import com.xing.crm.service.ProRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.L
 * @version V1.0
 * @date 2021年08月04日 16:44
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ProRoleServiceImpl implements ProRoleService {
	
	@Resource
	private ProRoleMapper proRoleMapper;
	
	@Override
	public PageResult<ProRole> list(int page, int limit, ProRole proRole) {
		Map params = new HashMap(0);
		params.put("start", (page - 1) * limit);
		params.put("pageSize", limit);
		params.put("role", proRole);
		List<ProRole> roles = proRoleMapper.list(params);
		PageResult<ProRole> result = new PageResult();
		result.setCode(0);
		result.setCount(proRoleMapper.count(params));
		result.setData(roles);
		result.setMsg("查询成功");
		return result;
	}
	
	@Override
	public List<ProRole> getRoles() {
		
		return proRoleMapper.getRoles();
	}
	
	@Override
	public ProRole getUserRole(int uid) {
		return proRoleMapper.getUserRole(uid);
	}
	
	@Override
	public ProRole get(Integer id) {
		ProRole role = null;
		if(id != null) {
			role = proRoleMapper.get(id);
		}
		return role;
	}
	
	@Override
	public Map<String, Object> edit(ProRole proRole) {
		Map result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "保存成功");
		if(proRole.getId() == null) {
			Integer id = proRoleMapper.insert(proRole);
			proRole.setId(id);
		} else {
			proRoleMapper.edit(proRole);
		}
		result.put("data", proRole);
		return result;
	}
	
	@Override
	public Map<String, Object> delete(Integer id) {
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "执行成功");
		int success = 0;
		if(id != null) {
			/* 删除关联表信息 */
			proRoleMapper.delRelationData(id);
			success = proRoleMapper.delete(id);
		}
		result.put("data", success);
		return result;
	}
	
	@Override
	public int delUserRole(Integer uid) {
		return proRoleMapper.delUserRole(uid);
	}
	
	@Override
	public int deleteShopUsersRole(Integer uid) {
		return proRoleMapper.deleteShopUsersRole(uid);
	}
}
